<?php
/**
 * 
 * 个人设置 处理类
 * @author tanzhe
 *
 */
class HomeModel {
	private $db;
	private $cache;
	public function __construct() {
		$this->db = new Data();
		$this->cache = new Cache();
	}
	
	public function getClothes($key) { //获得已有的所有衣服
	 	$playerInfo = $this->cache->get('ku_'.$key);
		$chkRet = Common::checkPlayerInfo($playerInfo);
		if($chkRet != 0) {
			return array('code'=>$chkRet,'ret'=>'');
		}
		$myClothes = $this->cache->get('my_clothes_'.$playerInfo['u_id']);
		if ( empty($myClothes) ) {
			$query = $this->db->query("select `label`,`hotness` from player_clothes where `u_id`={$userId} order by hotness, `hotness_type` desc");
			$data = $this->db->fetchAllByQuery($query);
			$myClothes = array();
			foreach ( $data as $v ) {
				$myClothes[] = $v['label'];
			}
			$this->cache->set('my_clothes_'.$playerInfo['u_id'], $myClothes);
		}
		arsort($myClothes);
		return array('code'=>0,'ret'=>array('my_clothes'=>$myClothes));
	}

	/**
	 * 
	 * 换装
	 * @param str $key
	 * @param array $clothes 衣服信息
	 */
	function saveClothes($key, $clothes) {
		$playerInfo = $this->cache->get('ku_'.$key); //用户是否登录
	
		$chkRet = Common::checkPlayerInfo($playerInfo);
		if($chkRet != 0) {
			return array('code'=>$chkRet,'ret'=>'');
		}
		$aryClothes = empty($clothes) ? array() : json_decode($clothes, true);
		//验证比穿项
		//1、连衣裙 2、外衣 3、内衣 4、裙子裤子 5、首饰 6、挎包 7、鞋子
		if ( empty($aryClothes[7]) ) {
			return array('code'=>216,'ret'=>'');
		}
		if ( empty($aryClothes[1]) ) {
			if ( empty($aryClothes[3]) ) {
				return array('code'=>217,'ret'=>'');
			}
			if ( empty($aryClothes[4]) ) {
				return array('code'=>218,'ret'=>'');
			}
		}
		//判断互斥
		if ( !empty($aryClothes[1]) ) {
			if ( !empty($aryClothes[3]) ) {
				return array('code'=>219,'ret'=>'');
			}
			if ( !empty($aryClothes[4]) ) {
				return array('code'=>220,'ret'=>'');
			}
		}
		//验证衣服是否属于自己的
		$myClothes = $this->cache->get('my_clothes_'.$playerInfo['u_id']);
		if ( empty($myClothes) ) {
			return array('code'=>2,'ret'=>'');
		}
		foreach ( $aryClothes as $v ) {
			if ( !empty($v) && !array_key_exists($v, $myClothes) ) {
				return array('code'=>221,'ret'=>''); 
			}
		}
		//重新计算工作属性与夜店属性
		$workHotness = 0;
		$clubHotness = 0;
		$clothesLabel = array();
		foreach ( $aryClothes as $v ) {
			if ( !empty($v) ){
				$clothesData = $this->cache->get('clothes_'.$v);
				if ( empty($clothesData) ) {
					$query = $this->db->query("select * from clothes where `label`='{$v}'");
					$clothesData = $this->db->fetchOneByQuery($query);
					$this->cache->set('clothes_'.$v, $clothesData);
				}
				if ( empty($clothesData) ) { //不存在该衣服
					return array('code'=>207,'ret'=>'');
				}
				if ( $clothesData['hotness_type'] == 1 ) {
					$workHotness += $clothesData['hotness'];
				} elseif ( $clothesData['hotness_type'] == 2 ) {
					$clubHotness += $clothesData['hotness'];
				} else {
					$workHotness += $clothesData['hotness'];
					$clubHotness += $clothesData['hotness'];
				}
				$clothesLabel[] = $v;
			}
		}
		
		//更新数据库与缓存
		$updateData = array();
		$updateData['clothes'] = serialize($aryClothes);
		$updateData['work_hotness'] = $workHotness;
		$updateData['club_hotness'] = $clubHotness;
		$playerInfo['clothes'] = $aryClothes;
		$playerInfo['work_hotness'] = $workHotness;
		$playerInfo['club_hotness'] = $clubHotness;
		//更新角色信息
		$options = array(
			'key'=>array(
				'u_id'=>$playerInfo['u_id'],
			),
			'data'=>$updateData,
		);
		$this->db->updateData('player_info', $options);
		$this->cache->set("ku_".$key, $playerInfo);
		//更新衣服表
		//$aryClothes = array_diff($aryClothes,array('',' ',0,null));
		foreach ( $aryClothes as $k=>$v ) {
			if ( empty($v) ) {
				unset($aryClothes[$k]);
			} else {
				$aryClothes[$k] = "'{$v}'";
			}
		}
		$strClothes = implode(',', $aryClothes);
		$updateSql = "update player_clothes set `is_now`=1 where `u_id`={$playerInfo['u_id']} and `label` in({$strClothes})";
		$this->db->query($updateSql);
		//返回个人信息
		$ret = array(
			'work_hotness'=>$playerInfo['work_hotness'],
			'club_hotness'=>$playerInfo['club_hotness'],
		);
		return array('code'=>0,'ret'=>$ret);
	}
}
?>